package com.li.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PolanNoation {
    public static void main(String[] args) {
        //定义一个逆波兰表达式
        String ex = "1+((2+3)*4)-5";
        //1先将ex放入Arraylist中
        //2讲Arraylist传递一个方法，配合栈进行运算
        List<String> list = toindex(ex);
        System.out.println("转换前" + list);
        List<String> list2 = parse(list);
        System.out.println(list2);

    }

    public static List<String> parse(List<String> ls) {
        Stack<String> s1 = new Stack<String>();//符号栈
        List<String> s2 = new ArrayList<String>();
        for (String item : ls) {
            //如果是一个数字，加入s2
            if (item.matches("\\d+")) {
                s2.add(item);
            } else if (item.equals("(")) {
                s1.push(item);
            } else if (item.equals(")")) {
                while (!s1.peek().equals("(")) {
                    s2.add(s1.pop());
                }
                s1.pop();
            } else {
                while (s1.size() != 0 && opera.getvalue(s1.peek()) >= opera.getvalue(item)) {
                    s2.add(s1.pop());
                }
                s1.push(item);
            }
        }
        while (s1.size() != 0) {
            s2.add(s1.pop());
        }
        return s2;
    }

    public static List<String> toindex(String s) {
        List<String> ls = new ArrayList<String>();
        int i = 0;
        String str;
        char c;
        do {
            if ((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57) {
                ls.add("" + c);
                i++;
            } else {
                str = "";
                while (i < s.length() && (c = s.charAt(i)) <= 57 && (c = s.charAt(i)) >= 48) {
                    str += c;
                    i++;
                }
                ls.add(str);

            }
        } while (i < s.length());
        return ls;
    }

    //讲一个逆波兰表达式，依次讲数组和表达式放入数组
    public static List<String> getList(String sufferex) {
        String[] split = sufferex.split(" ");
        List<String> list = new ArrayList<String>();
        for (String els : split) {
            list.add(els);
        }
        return list;
    }

    public static int calcute(List<String> ls) {
        Stack<String> stack = new Stack<>();
        for (String item : ls) {
            if (item.matches("\\d+")) {//匹配是多位数
                stack.push(item);
            } else {
                //pop出两个数，运算，再运算
                int num2 = Integer.parseInt(stack.pop());
                int num1 = Integer.parseInt(stack.pop());
                int res = 0;
                if (item.equals("+")) {
                    res = num1 + num2;
                } else if (item.equals("-")) {
                    res = num1 - num2;
                } else if (item.equals("*")) {
                    res = num1 * num2;
                } else if (item.equals("/")) {
                    res = num1 / num2;
                } else {
                    throw new RuntimeException("运算符有无");
                }
                //把res入栈
                stack.push(res + "");
            }
        }
        return Integer.parseInt(stack.pop());
    }
}

class opera {
    public static int add = 1;
    public static int sub = 1;
    public static int muil = 2;
    public static int div = 2;

    public static int getvalue(String tiem) {
        int res = 0;
        switch (tiem) {
            case "+":
                res = add;
                break;
            case "-":
                res = sub;
                break;
            case "*":
                res = muil;
                break;
            case "/":
                res = div;
                break;
            default:
        }
        return res;
    }

}
